<template>
  <el-dialog
    :title="!dataForm.customerId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户登录名" prop="loginName">
      <el-input v-model="dataForm.loginName" placeholder="用户登录名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobilePhone">
      <el-input v-model="dataForm.mobilePhone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="用户地址" prop="addr">
      <el-input v-model="dataForm.addr" placeholder="用户地址"></el-input>
    </el-form-item>
    <el-form-item label="用户状态" prop="userStats">
      <el-input v-model="dataForm.userStats" placeholder="用户状态"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="customerImg">
      <el-input v-model="dataForm.customerImg" placeholder="用户头像"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          customerId: 0,
          loginName: '',
          password: '',
          mobilePhone: '',
          gender: '',
          addr: '',
          userStats: '',
          userName: '',
          customerImg: ''
        },
        dataRule: {
          loginName: [
            { required: true, message: '用户登录名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          mobilePhone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          addr: [
            { required: true, message: '用户地址不能为空', trigger: 'blur' }
          ],
          userStats: [
            { required: true, message: '用户状态不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          customerImg: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.customerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.customerId) {
            this.$http({
              url: this.$http.adornUrl(`/shop/customer/info/${this.dataForm.customerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.loginName = data.customer.loginName
                this.dataForm.password = data.customer.password
                this.dataForm.mobilePhone = data.customer.mobilePhone
                this.dataForm.gender = data.customer.gender
                this.dataForm.addr = data.customer.addr
                this.dataForm.userStats = data.customer.userStats
                this.dataForm.userName = data.customer.userName
                this.dataForm.customerImg = data.customer.customerImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/customer/${!this.dataForm.customerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'customerId': this.dataForm.customerId || undefined,
                'loginName': this.dataForm.loginName,
                'password': this.dataForm.password,
                'mobilePhone': this.dataForm.mobilePhone,
                'gender': this.dataForm.gender,
                'addr': this.dataForm.addr,
                'userStats': this.dataForm.userStats,
                'userName': this.dataForm.userName,
                'customerImg': this.dataForm.customerImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
